import { defineStore } from "pinia";
import type { IProduct } from "@/server/api/products";

const UseShopStore = defineStore("shop", {
  state: () => {
    return {
      products: [] as Array<IProduct>,
    };
  },
  actions: {
    async InitProducts() {
      const { data } = await useFetch("http://localhost:3000/api/products");

      this.products = data.value;
    },
    DecProQuantity(id: number) {
      const res: IProduct = this.products.find((item) => item.id == id);
      if (res.inventory <= 0) return // 小于等于0，退出
      res.inventory--
    },
  },
});

export { UseShopStore };
